package com.steven.im.utils;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;

import java.io.IOException;
import java.util.List;

public class GetAddressUtil {
    Context context;

    GetAddressUtil(Context context) {
        this.context = context;
    }

    public String getAddress(double lnt , double lat){

        Geocoder geocoder = new Geocoder(context);
        StringBuilder stringBuilder = new StringBuilder();
        try {

            //根据经纬度获取地理位置信息---这里会获取最近的几组地址信息，具体几组由最后一个参数决定
            List<Address> addresses = geocoder.getFromLocation(lat , lnt, 1);

            if (addresses.size() > 0) {
                Address address = addresses.get(0);
                for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                    //每一组地址里面还会有许多地址。这里我取的前2个地址。xxx街道-xxx位置
                    if(i == 0) {
                        stringBuilder.append(address.getAddressLine(i)).append("-");
                    }

                    if(i == 1){
                        stringBuilder.append(address.getAddressLine(i));
                        break;
                    }
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return stringBuilder.toString();

    }
}
